<template>
  <div>
    <div class="list-search">
      <ul class="search-list">
        <li style="float:left; margin-right:auto;">
          <el-button
            type="success"
            icon="el-icon-plus"
            plain
            @click="showDialog('add')"
            >添加户籍信息</el-button
          >
        </li>
        <li>
          <el-input
            v-model="searchList.name"
            placeholder="请输入户主姓名"
          ></el-input>
        </li>
        <li>
          <el-input
            v-model="searchList.mobile"
            placeholder="请输入户主手机号"
          ></el-input>
        </li>
        <li>
          <el-date-picker
            v-model="searchList.selectTime"
            value-format="yyyy-MM-dd HH:mm"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </li>
        <li>
          <el-button
            type="primary"
            icon="el-icon-search"
            plain
            @click="search()"
            >查询</el-button
          >
        </li>
      </ul>
    </div>
    <el-table :data="houseList" border v-loading="loading" style="width: 100%">
      <el-table-column prop="name" label="户主"> </el-table-column>
      <el-table-column prop="mobile" label="户主手机号"> </el-table-column>
      <el-table-column prop="nativePlace" label="籍贯"> </el-table-column>
      <el-table-column prop="address" label="现住址"> </el-table-column>
      <el-table-column label="所在社区">
        <template slot-scope="scope">
          {{ communityMap[scope.row.community] }}
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="户籍状态">
        <template slot-scope="scope">
          {{houseTypeMap[scope.row.houseType]}}
        </template>
      </el-table-column> -->
      <el-table-column prop="created" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.houseType == '0'"
            type="primary"
            size="mini"
            @click="detail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="pageNum"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { storage } from "../../utils/storage";
export default {
  data() {
    return {
      houseList: [],
      loading: false,
      searchList: {
        name: "",
        mobile: "",
        selectTime: null
      },
      communityMap: {},
      communityOptions: [],
      houseTypeMap: {
        //户型状态
        "0": "正常",
        "1": "已迁出",
        "2": "迁入"
      },
      total: 0,
      pageNum: 1,
      pageSize: 5
    };
  },
  methods: {
    getTimes(str) {
      return new Date(str).getTime();
    },
    getList(n, isSearch) {
      // 获取户籍列表
      this.loading = true;
      var cloneList = storage.getStorage("house");
      if (isSearch) {
        if (this.searchList.name != "") {
          cloneList = cloneList.filter(
            item => item.name == this.searchList.name
          );
        }
        if (this.searchList.mobile != "") {
          cloneList = cloneList.filter(
            item => item.mobile == this.searchList.mobile
          );
        }
        if (this.searchList.selectTime != null) {
          cloneList = cloneList.filter(
            item =>
              this.getTimes(item.created) >=
                this.getTimes(this.searchList.selectTime[0]) &&
              this.getTimes(item.created) <=
                this.getTimes(this.searchList.selectTime[1])
          );
        }
      }
      var resList =
        cloneList.length - (n - 1) * this.pageSize >= this.pageSize
          ? cloneList.slice((n - 1) * this.pageSize, this.pageSize * n)
          : cloneList.slice((n - 1) * this.pageSize);
      setTimeout(() => {
        this.houseList = resList;
        this.total = cloneList.length;
        this.pageNum = n;
        this.loading = false;
      }, 500);
    },
    search() {
      if (
        this.searchList.name == "" &&
        this.searchList.mobile == "" &&
        this.searchList.selectTime == null
      ) {
        return this.getList(1);
      }
      this.getList(1, true);
    },
    getCommunityMap() {
      storage.getStorage("community").forEach((ele, index) => {
        this.communityMap[ele.id] = ele.name;
      });
    },
    detail(temp) {
      this.$router.push(`./household/detail/${temp.houseId}`);
    },
    showDialog(type, index) {
      this.$router.push("/household/add");
    },
    handleCurrentChange(e) {
      this.pageNum = e;
      if (
        this.searchList.name == "" &&
        this.searchList.mobile == "" &&
        this.searchList.selectTime == null
      ) {
        return this.getList(e);
      } else {
        this.getList(e, true);
      }
    }
  },
  mounted() {
    this.getList(1);
    this.getCommunityMap();
  }
};
</script>

<style scoped>
.el-input {
  width: 220px;
}
</style>
